var geocoder;
var infowindow = new google.maps.InfoWindow();
var encodedPath;
var polyLine;

var myOptions = {
	center : new google.maps.LatLng(-8.0464492, -34.9324882),
	zoom : 8,
};

var drawingManager = new google.maps.drawing.DrawingManager({
	drawingMode : google.maps.drawing.OverlayType.MARKER,
	drawingControl : true,
	drawingControlOptions : {
		position : google.maps.ControlPosition.TOP_CENTER,
		drawingModes : [ google.maps.drawing.OverlayType.POLYLINE ]
	}
});

function initialize() {
	geocoder = new google.maps.Geocoder();
	map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
	drawingManager.setMap(map);

	google.maps.event.addListener(drawingManager, 'overlaycomplete', function(
			event) {
		if (event.type == google.maps.drawing.OverlayType.POLYLINE) {

			polyLine = event.overlay;
			encodedPath = google.maps.geometry.encoding.encodePath(polyLine
					.getPath());
			polyLine.content = encodedPath;

			google.maps.event.addListener(polyLine, 'click', function(event) {
				infowindow.setContent(this.content);
				infowindow.setPosition(event.latLng);
				infowindow.open(map);
			});
		}
	});
}

function getUrlMap() {
	var url = "http://maps.googleapis.com/maps/api/staticmap?size=1024x480&amp;path=weight:3%7Cenc:"
			.concat(encodedPath);
	document.getElementById('form:urlMap').value = url;
}

function codeAddress() {
	var address = document.getElementById('address').value;
	geocoder.geocode({
		'address' : address
	}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK) {
			map.setCenter(results[0].geometry.location);
			var marker = new google.maps.Marker({
				map : map,
				position : results[0].geometry.location
			});
		} else {
			alert('Geocode was not successful for the following reason: '
					+ status);
		}
	});
}
google.maps.event.addDomListener(window, 'load', initialize);